# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=tk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=8.6.10
_patch=
pkgver=${_basever}${_patch}
pkgrel=1
pkgdesc="A windowing toolkit for use with tcl (mingw-w64)"
arch=('any')
url="https://tcl.sourceforge.io/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-tcl>=${_basever}")
options=('staticlibs') # '!strip' 'debug')
source=("https://downloads.sourceforge.net/sourceforge/tcl/${_realname}${pkgver}-src.tar.gz"
        002-implib-name.mingw.patch
        003-fix-forbidden-colon-in-paths.mingw.patch
        004-install-man.mingw.patch
        006-prevent-tclStubsPtr-segfault.patch
        008-dont-link-shared-with--static-libgcc.patch)
sha256sums=('63df418a859d0a463347f95ded5cd88a3dd3aaa1ceecaeee362194bc30f3e386'
            '441f2f5bdf1ee2bf6697569365207d554130bd5a2bac01e10a6e1a37738d8006'
            '5347487af0e736dbb51425b22ed308840faf75b44c070623baa55f78dac3d053'
            '8516749dd73c084ece7b9df6d1ba5708e652e8ba39cad59120c7f909f61747f0'
            '0029fde6782ce7635e9957412d3e36a7e76d304399d57d64a42818f93e705621'
            'a16406e8519ab681bba0915e35b23a7f91cf978934b0a4ebdc7e949e87f0c877')

prepare() {
  cd "${srcdir}/${_realname}${_basever}"
  patch -p1 -i "${srcdir}"/002-implib-name.mingw.patch
  patch -p1 -i "${srcdir}"/003-fix-forbidden-colon-in-paths.mingw.patch
  patch -p1 -i "${srcdir}"/004-install-man.mingw.patch
  patch -p1 -i "${srcdir}"/006-prevent-tclStubsPtr-segfault.patch
  patch -p1 -i "${srcdir}"/008-dont-link-shared-with--static-libgcc.patch

  cd win && autoreconf -fi
}

build() {
   local extra_config=
  if check_option "debug" "y"; then
    extra_config="--enable-symbols"
  fi
  local enable64bit=
  [[ "${MINGW_CHOST}" = 'x86_64-w64-mingw32' ]] && enable64bit='--enable-64bit'

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${srcdir}/tk${_basever}/win/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-tcl=${MINGW_PREFIX}/lib \
    ${extra_config} \
    ${enable64bit}

  make
}

package() {
  local debug_suffix=
  if check_option "debug" "y"; then
    debug_suffix="g"
  fi
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install INSTALL_ROOT="${pkgdir}"
  cp "${pkgdir}${MINGW_PREFIX}/bin/wish86${debug_suffix}.exe" "${pkgdir}${MINGW_PREFIX}/bin/wish.exe"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  find "${pkgdir}${MINGW_PREFIX}" -name '*.sh' -o -name '*.dll' -o -name '*.exe' | xargs -rtl1 chmod 755

  ln -s "${pkgdir}${MINGW_PREFIX}/lib/libtk86${debug_suffix}.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libtk.dll.a"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private/"{generic/ttk,win}

  cd ${srcdir}/tk${_basever}
  find generic win -name "*.h" -exec cp -p '{}' "${pkgdir}${MINGW_PREFIX}"/include/tk${_basever%.*}/tk-private/'{}' ';'
  ( cd "${pkgdir}${MINGW_PREFIX}/include"
    for header in *.h ; do
      cp -f ${header} ${pkgdir}${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private/generic/
    done
  )
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/"*/pkgIndex.tcl

  local _libver=${_basever%.*}
  _libver=${_libver//./}
  sed -i \
    -e "s|^\(TK_BUILD_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtk${_libver}.dll.a'|" \
    -e "s|^\(TK_SRC_DIR\)='.*'|\1='${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib -ltkstub${_libver}'|" \
    ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  # Add missing entry to tkConfig.sh
  echo "# String to pass to the compiler so that an extension can" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "# find installed Tcl headers." >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "TK_INCLUDE_SPEC='-I${MINGW_PREFIX}/include/tk${_basever%.*}'" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  rm "${pkgdir}${MINGW_PREFIX}/lib/tk${_basever%.*}/tkAppInit.c"
}
